﻿using System;
using System.Globalization;
using System.Threading;
using System.Web;
using AutoMapper;
using Core;
using IglooCoder.Commons.WcfNhibernate;
using WcfService1.DTO;

namespace WcfService1
{
    public class Global : HttpApplication
    {
      
        protected void Application_Start(object sender, EventArgs e)
        {
            System.Net.ServicePointManager.DefaultConnectionLimit = 1000;
            ThreadPoolTimeoutWorkaround.DoWorkaround();
            NHibernateFactory.Initialize();
            CreateMapping();
            
           
        }

        protected void Session_Start(object sender, EventArgs e)
        {
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            //ThreadPoolTimeoutWorkaround.DoWorkaround();
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
        }

        protected void Application_Error(object sender, EventArgs e)
        {
        }

        protected void Session_End(object sender, EventArgs e)
        {
        }

        protected void Application_End(object sender, EventArgs e)
        {
        }


        private void CreateMapping()
        {
            Mapper.CreateMap(typeof (Inventory), typeof (InventoryDTO));
            Mapper.CreateMap(typeof (InventoryDTO), typeof (Inventory));
            Mapper.CreateMap(typeof (User), typeof (UserDTO));
            Mapper.CreateMap(typeof (UserDTO), typeof (User));
            Mapper.CreateMap(typeof (Group), typeof (GroupDTO));
            Mapper.CreateMap(typeof (GroupDTO), typeof (Group));
            Mapper.CreateMap(typeof (SubGroup), typeof (SubGroupDTO));
            Mapper.CreateMap(typeof (SubGroupDTO), typeof (SubGroup));
            Mapper.CreateMap(typeof (Item), typeof (ItemDTO));
            Mapper.CreateMap(typeof (ItemDTO), typeof (Item));
            Mapper.CreateMap(typeof (Menu), typeof (MenuDTO));
            Mapper.CreateMap(typeof (MenuDTO), typeof (Menu));
            Mapper.CreateMap(typeof (Role), typeof (RoleDTO));
            Mapper.CreateMap(typeof (RoleDTO), typeof (Role));
            Mapper.CreateMap(typeof (Satuan), typeof (SatuanDTO));
            Mapper.CreateMap(typeof (SatuanDTO), typeof (Satuan));
            Mapper.CreateMap(typeof (Warehouse), typeof (WarehouseDTO));
            Mapper.CreateMap(typeof (WarehouseDTO), typeof (Warehouse));
            //Mapper.CreateMap(typeof (Pallet), typeof (PalletDTO));
            //Mapper.CreateMap(typeof (PalletDTO), typeof (Pallet));
            Mapper.CreateMap(typeof (Supplier), typeof (SupplierDTO));
            Mapper.CreateMap(typeof (SupplierDTO), typeof (Supplier));
            Mapper.CreateMap(typeof (Satuan), typeof (SatuanDTO));
            Mapper.CreateMap(typeof (SatuanDTO), typeof (Satuan));
            Mapper.CreateMap(typeof (COA), typeof (CoaDTO));
            Mapper.CreateMap(typeof (CoaDTO), typeof (COA));
            Mapper.CreateMap(typeof (PurchaseOrder), typeof (PurchaseOrderDTO));
            Mapper.CreateMap(typeof (PurchaseOrderDTO), typeof (PurchaseOrder));
            Mapper.CreateMap(typeof (PurchaseOrderDetail), typeof (PurchaseOrderDetailDTO));
            Mapper.CreateMap(typeof (PurchaseOrderDetailDTO), typeof (PurchaseOrderDetail));
            Mapper.CreateMap(typeof (Currency), typeof (CurrencyDTO));
            Mapper.CreateMap(typeof (CurrencyDTO), typeof (Currency));
            Mapper.CreateMap(typeof (Customer), typeof (CustomerDTO));
            Mapper.CreateMap(typeof (CustomerDTO), typeof (Customer));
            Mapper.CreateMap(typeof (BTB), typeof (BTBDTO));
            Mapper.CreateMap(typeof (BTBDTO), typeof (BTB));
            Mapper.CreateMap(typeof (BTBDetail), typeof (BTBDetailDTO));
            Mapper.CreateMap(typeof (BTBDetailDTO), typeof (BTBDetail));
            Mapper.CreateMap(typeof (PurchaseReturn), typeof (PurchaseReturnDTO));
            Mapper.CreateMap(typeof (PurchaseReturnDTO), typeof (PurchaseReturn));
            Mapper.CreateMap(typeof (PurchaseReturnDetail), typeof (PurchaseReturnDetailDTO));
            Mapper.CreateMap(typeof (PurchaseReturnDetailDTO), typeof (PurchaseReturnDetail));
            Mapper.CreateMap(typeof (DeliveryType), typeof (DeliveryTypeDTO));
            Mapper.CreateMap(typeof (DeliveryTypeDTO), typeof (DeliveryType));
            Mapper.CreateMap(typeof (SalesOrder), typeof (SalesOrderDTO));
            Mapper.CreateMap(typeof (SalesOrderDTO), typeof (SalesOrder));
            Mapper.CreateMap(typeof (SalesOrderDetail), typeof (SalesOrderDetailDTO));
            Mapper.CreateMap(typeof (SalesOrderDetailDTO), typeof (SalesOrderDetail));
            Mapper.CreateMap(typeof (DeliveryOrder), typeof (DeliveryOrderDTO));
            Mapper.CreateMap(typeof (DeliveryOrderDTO), typeof (DeliveryOrder));
            Mapper.CreateMap(typeof (DeliveryOrderDetail), typeof (DeliveryOrderDetailDTO));
            Mapper.CreateMap(typeof (DeliveryOrderDetailDTO), typeof (DeliveryOrderDetail));
            Mapper.CreateMap(typeof (SalesInvoiceDTO), typeof (SalesInvoice));
            Mapper.CreateMap(typeof (SalesInvoice), typeof (SalesInvoiceDTO));
            Mapper.CreateMap(typeof (SalesInvoiceDetail), typeof (SalesInvoiceDetailDTO));
            Mapper.CreateMap(typeof (SalesInvoiceDetailDTO), typeof (SalesInvoiceDetail));
            Mapper.CreateMap(typeof (SalesReturn), typeof (SalesReturnDTO));
            Mapper.CreateMap(typeof (SalesReturnDTO), typeof (SalesReturn));
            Mapper.CreateMap(typeof (SalesReturnDetail), typeof (SalesReturnDetailDTO));
            Mapper.CreateMap(typeof (SalesReturnDetailDTO), typeof (SalesReturnDetail));
            Mapper.CreateMap(typeof (BTBDetail), typeof (Inventory)).ForMember("SerialNumber", x => x.MapFrom("NoLog"));
            Mapper.CreateMap(typeof (BtbElseDetail), typeof (Inventory)).ForMember("SerialNumber",
                                                                                   x => x.MapFrom("NoLog"));
            Mapper.CreateMap(typeof (PurchaseInvoice), typeof (PurchaseInvoiceDTO));
            Mapper.CreateMap(typeof (PurchaseInvoiceDTO), typeof (PurchaseInvoice));
            Mapper.CreateMap(typeof (PurchaseInvoiceDetail), typeof (PurchaseInvoiceDetailDTO));
            Mapper.CreateMap(typeof (PurchaseInvoiceDetailDTO), typeof (PurchaseInvoiceDetail));
            Mapper.CreateMap(typeof (MutasiWarehouse), typeof (MutasiWarehouseDTO));
            Mapper.CreateMap(typeof (MutasiWarehouseDTO), typeof (MutasiWarehouse));
            Mapper.CreateMap(typeof (MutasiWarehouseDetail), typeof (MutasiWarehouseDetailDTO));
            Mapper.CreateMap(typeof (MutasiWarehouseDetailDTO), typeof (MutasiWarehouseDetail));
            //Mapper.CreateMap(typeof (MutasiPallet), typeof (MutasiPalletDTO));
            //Mapper.CreateMap(typeof (MutasiPalletDTO), typeof (MutasiPallet));
            Mapper.CreateMap(typeof (MutasiPalletDetail), typeof (MutasiPalletDetailDTO));
            Mapper.CreateMap(typeof (MutasiPalletDetailDTO), typeof (MutasiPalletDetail));
            Mapper.CreateMap(typeof (BtbElse), typeof (BtbElseDTO));
            Mapper.CreateMap(typeof (BtbElseDTO), typeof (BtbElse));
            Mapper.CreateMap(typeof (BtbElseDetail), typeof (BtbElseDetailDTO));
            Mapper.CreateMap(typeof (BtbElseDetailDTO), typeof (BtbElseDetail));
            Mapper.CreateMap(typeof (DeliveryOrderElse), typeof (DeliveryOrderElseDTO));
            Mapper.CreateMap(typeof (DeliveryOrderElseDTO), typeof (DeliveryOrderElse));
            Mapper.CreateMap(typeof (DeliveryOrderElseDetail), typeof (DeliveryOrderElseDetailDTO));
            Mapper.CreateMap(typeof (DeliveryOrderElseDetailDTO), typeof (DeliveryOrderElseDetail));
            Mapper.CreateMap(typeof (PreSalesOrder), typeof (PreSalesOrderDTO));
            Mapper.CreateMap(typeof (PreSalesOrderDTO), typeof (PreSalesOrder));
            Mapper.CreateMap(typeof (PreSalesOrderDetail), typeof (PreSalesOrderDetailDTO));
            Mapper.CreateMap(typeof (PreSalesOrderDetailDTO), typeof (PreSalesOrderDetail));
            Mapper.CreateMap(typeof (JurnalConfig), typeof (JurnalConfigDTO));
            Mapper.CreateMap(typeof (JurnalConfigDTO), typeof (JurnalConfig));
            Mapper.CreateMap(typeof (Spk), typeof (SpkDTO));
            Mapper.CreateMap(typeof (SpkDTO), typeof (Spk));
            Mapper.CreateMap(typeof (SpkDetail), typeof (SpkDetailDTO));
            Mapper.CreateMap(typeof (SpkDetailDTO), typeof (SpkDetail));
            Mapper.CreateMap(typeof (InventoryAdjustment), typeof (InventoryAdjustmentDTO));
            Mapper.CreateMap(typeof (InventoryAdjustmentDTO), typeof (InventoryAdjustment));
            Mapper.CreateMap(typeof (InventoryAdjustmentDetail), typeof (InventoryAdjustmentDetailDTO));
            Mapper.CreateMap(typeof (InventoryAdjustmentDetailDTO), typeof (InventoryAdjustmentDetail));
            Mapper.CreateMap(typeof (AppParamDTO), typeof (AppParam));
            Mapper.CreateMap(typeof (AppParam), typeof (AppParamDTO));
            Mapper.CreateMap(typeof (CostCenter), typeof (CostCenterDTO));
            Mapper.CreateMap(typeof (CostCenterDTO), typeof (CostCenter));
            Mapper.CreateMap(typeof(Employee), typeof(EmployeeDTO));
            Mapper.CreateMap(typeof(EmployeeDTO), typeof(Employee));
            Mapper.CreateMap(typeof(Jurnal), typeof(JurnalDTO));
            Mapper.CreateMap(typeof(JurnalDTO), typeof(Jurnal));
            Mapper.CreateMap<JurnalDetail, JurnalDetailDTO>().ForMember(x=>x.Coa, o => o.MapFrom(x => new CoaDTO(){AccountNumber = x.AccountNumber}));
            Mapper.CreateMap<JurnalDetailDTO,JurnalDetail>().ForMember("AccountNumber",o=>o.MapFrom(x=>x.Coa.AccountNumber));
            Mapper.CreateMap(typeof(CashBankIn), typeof(CashBankInDTO));
            Mapper.CreateMap(typeof(CashBankInDTO), typeof(CashBankIn));
            Mapper.CreateMap(typeof(CashBankInDetail), typeof(CashBankInDetailDTO));
            Mapper.CreateMap(typeof(CashBankInDetailDTO), typeof(CashBankInDetail));
            Mapper.CreateMap(typeof(CashBankOut), typeof(CashBankOutDTO));
            Mapper.CreateMap(typeof(CashBankOutDTO), typeof(CashBankOut));
            Mapper.CreateMap(typeof(CashBankOutDetail), typeof(CashBankOutDetailDTO));
            Mapper.CreateMap(typeof(CashBankOutDetailDTO), typeof(CashBankOutDetail));

            //AutoMapper.Mapper.CreateMap(typeof(InventoryWarehouse), typeof(InventoryWarehouseDTO));
            //AutoMapper.Mapper.CreateMap(typeof(InventoryWarehouseDTO), typeof(InventoryWarehouse));

            //AutoMapper.Mapper.CreateMap<SalesOrder, SalesOrderDTO>().ForMember(x => x.SalesOrderDetails, a => a.Ignore());
        }


    }

    public class CustomResolver : ValueResolver<CoaDTO, String>
    {
        protected override string ResolveCore(CoaDTO source)
        {
            return source.AccountNumber;
        }
    }

 
    static class ThreadPoolTimeoutWorkaround
    {
        static ManualResetEvent s_dummyEvent;
        static RegisteredWaitHandle s_registeredWait;

        public static void DoWorkaround()
        {
            // Create an event that is never set
            s_dummyEvent = new ManualResetEvent(false);

            // Register a wait for the event, with a periodic timeout. This causes callbacks
            // to be queued to an IOCP thread, keeping it alive
            s_registeredWait = ThreadPool.RegisterWaitForSingleObject(
                s_dummyEvent,
                (a, b) =>
                {
                    // Do nothing
                },
                null,
                1000,
                false);
        }
    }
}